package com.jy.adscm.zltx.controller;


import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.QsApplyUpdateRecord;
import com.jy.adscm.zltx.service.QsApplyUpdateRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文件修改申请记录 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-10-23
 */
@RestController
@RequestMapping("/adscm/zltx/qsApplyUpdateRecord")
public class QsApplyUpdateRecordController {
    @Autowired
    QsApplyUpdateRecordService qsApplyUpdateRecordService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @AdscmLogger("新增文件修改申请记录")
    @RequestMapping("/insert")
    public SimpleCoreResponse insert(@RequestBody QsApplyUpdateRecord qsApplyUpdateRecord , HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (token==null){
            throw  new AdscmSystemException("没找到登录信息");
        }
        qsApplyUpdateRecord.setApplicant(token.getUser().getId().toString());
        qsApplyUpdateRecord.setApplyTime(new Date());
        qsApplyUpdateRecord.setIsFinishUpdate("0");
        qsApplyUpdateRecord.setApprovalResult("-1");
        qsApplyUpdateRecordService.insertQsApplyUpdateRecord(qsApplyUpdateRecord);
        return  new SimpleCoreResponse("200","新增成功",null);
    }
    @AdscmLogger("修改文件修改申请记录")
    @RequestMapping("/update")
    public  SimpleCoreResponse update( @RequestBody QsApplyUpdateRecord qsApplyUpdateRecord , HttpServletRequest request)throws AdscmSystemException{
        qsApplyUpdateRecordService.updateById(qsApplyUpdateRecord);
        return  new SimpleCoreResponse("200","修改成功",null);
    }
    @AdscmLogger("删除文件修改申请记录")
    @RequestMapping("/delete/{id}")
    public   SimpleCoreResponse delete(@PathVariable Integer id)throws AdscmSystemException{
        qsApplyUpdateRecordService.deleteById(id);
        return  new SimpleCoreResponse("200","删除成功",null);
    }
    @AdscmLogger("查询文件修改申请记录")
    @RequestMapping("/selectByFileId/{fileId}")
    public  SimpleCoreResponse seletByFileId(@PathVariable Integer fileId)throws AdscmSystemException{
        List<QsApplyUpdateRecord> qsApplyUpdateRecords = qsApplyUpdateRecordService.selectByFileId(fileId);
        return  new SimpleCoreResponse("200","查询成功",qsApplyUpdateRecords);
    }
}

